<?php
error_reporting(0);
$value1 = $_REQUEST['value1'];
$value2 = $_REQUEST['value2'];
$value3 = $_REQUEST['value3'];

$cf_action = isset($_POST['act']) ? trim($_POST['act']) : trim($_GET['act']);

switch ($cf_action) {
    case "getage":
        $time = strtotime($value2) - strtotime($value1);
        $year_sec = 3600 * 24 * 365;
        $year = round($time / $year_sec, 1);
        $date = explode(".", $year);
        $return['year'] = $date[0];
        $return['month'] = $date[1];
        echo json_encode($return);
        break;
    case "getdate":
        $time = strtotime($value2, strtotime($value1));
        //$time = strtotime("+10 days");
        $return = date("Y-m-d", $time);
        echo json_encode($return);
        //echo $time;
        break;
    case "ajaxupload":
        require(CONFIG_PATH . 'upload_setting.php');
        load_file('upload_class', LIB_PATH);			
        $dir = trim($_GET['dir']);
        $oldfile = trim(urldecode($_REQUEST['old']));
        $filename = trim(urldecode($_REQUEST['filename']));
        if (!array_key_exists($dir, $cf_upload_setting)) {
	    echo json_encode(array('error' => 1));
            exit;
        }
		
		
        //更小尺寸
        if ($_GET['size'] && $_GET['size'] < $cf_upload_setting[$dir]['size']) {
            $cf_upload_setting[$dir]['size'] = $_GET['size'];
        }

        if (strstr($_GET['thumb'], ",")) {
            $cf_upload_setting[$dir]['thumb'] = explode(",", $_GET['thumb']);
        }
        if (strstr($_GET['small'], ",")) {
            $cf_upload_setting[$dir]['small'] = explode(",", $_GET['small']);
        }

        //dump($filename);
        $datepath = ($cf_upload_setting[$dir]['datepath']===false) ? false : true;
        $setting = array( //参数设置
            'maxsize' => $cf_upload_setting[$dir]['size'], //最大文件大小 k为单位
            'type' => $cf_upload_setting[$dir]['allow'], //允许类型
            'datepath' => $datepath, //日期类型
            'filename' => array($filename), //文件名
            'upload_root' => UPLOAD_PATH . $dir . '/', //上传根目
        );
		//D:/AppServ/www/shoumedia/public/upload/ 
		
        $myupload = new MyUpload($setting);
        if ($myupload->check_form('imgFile')) {
            $output = $myupload->upload('imgFile');
            if ($output) { //上传成功
				
                $isimage = $myupload->checkimage();
                if ($isimage && ($cf_upload_setting[$dir]['thumb'] || $cf_upload_setting[$dir]['small'])) { //生成缩略图
                    
				
					$source = $myupload->get_output_file();
                    load_file('thumb_class', LIB_PATH);
                    $thumb = new ThumbHandler();
                    $thumb->setSrcImg($source[0]);
                    $thumb->setDstImg(get_thumb_name($source[0], 'thumb'));
                    $thumb->createImg($cf_upload_setting[$dir]['thumb'][0], $cf_upload_setting[$dir]['thumb'][1]);

                    if ($cf_upload_setting[$dir]['small']) {
                        $thumb->setSrcImg($source[0]);
                        $thumb->setDstImg(get_thumb_name($source[0], 'small'));
                        $thumb->createImg($cf_upload_setting[$dir]['small'][0], $cf_upload_setting[$dir]['small'][1]);
                    }
                }

                $rootfile = UPLOAD_URL . $dir . '/' . $output[0];
                if ($oldfile && $rootfile != $oldfile) {
                    $oldfile = ROOT_PATH . trim($oldfile, "/");
                    del_file($oldfile);
                    del_file(get_thumb_name($oldfile, 'thumb'));
                    del_file(get_thumb_name($oldfile, 'small'));
                }

            }
        }
        //dump($output);

        $filename = UPLOAD_URL . $dir . '/' . $output[0];
		
			$new_array = "\n"."\n".date("Y-m-d H:i:s")." AJAXJSON : $php_path ".json_encode(array('error' => 0, 'url' => $filename))."\n ";
		$new_array .= var_export($_GET,true);
		write_file('D:/AppServ/www/d2ziran/info/log/'.date("ym").'-editor.log',$new_array,"log");
        echo json_encode(array('error' => 0, 'url' => $filename));
		exit;
        break;
		
		
	case "ajaxdel":
		  $oldfile = ROOT_PATH . trim($value1, "/");
			 if(is_file($oldfile)){
				 del_file($oldfile);
				 del_file(get_thumb_name($oldfile, 'thumb'));
				 del_file(get_thumb_name($oldfile, 'small'));
		 	}
	break;
    case "upfile":								//有进度条upload,建议用于上传文件类型
	require(CONFIG_PATH . 'upload_setting.php');
        load_file('upload_class', LIB_PATH);

        $dir = trim($_POST['dir']);
        $oldfile = trim(urldecode($_POST['old']));
        $filename = trim(urldecode($_POST['filename']));
        if (!array_key_exists($dir, $cf_upload_setting)) {
	    echo json_encode(array('error' => '1','errortitle'=>'目录不存在'));
            exit;
        }
	
	$datepath = ($cf_upload_setting[$dir]['datepath']===false) ? false : true;
        $setting = array( //参数设置
            'maxsize' => $cf_upload_setting[$dir]['size'], //最大文件大小 k为单位
            'type' => $cf_upload_setting[$dir]['allow'], //允许类型
            'datepath' => $datepath, //日期类型
            'filename' => array($filename), //文件名
            'upload_root' => UPLOAD_PATH . $dir . '/', //上传根目
        );
	
	
        $myupload = new MyUpload($setting);
        if ($myupload->check_form('Filedata')) {
            $output = $myupload->upload('Filedata');
            if ($output) { //上传成功
                $rootfile = UPLOAD_URL . $dir . '/' . $output[0];
                if ($oldfile && $rootfile != $oldfile) {
                    $oldfile = ROOT_PATH . trim($oldfile, "/");
                    del_file($oldfile);
                }
		
		$filename = UPLOAD_URL . $dir . '/' . $output[0];
		echo json_encode(array('error' => 0, 'url' => $filename));    
            }
        }else{
	   echo json_encode(array('error' => 1, 'errortitle' => '上传失败')); 
	}
        break;
    case "delfile":
        return del_file(UPLOAD_FILE . $value1);
        break;
    case "searchuser":
		$key = strtolower($_GET["q"]);
		$sql = "SELECT user_id,user_name,user_pname,user_status FROM `data_user` WHERE user_name LIKE '%".$key."%'";
		$data = $db->getAll($sql);
		if($data){
			foreach($data as $k=>$v){				
				$status = $cf_info_var_common['user_status'][$v['user_status']];				
				echo $v['user_name'].'|'.$v['user_id'].'|'.$v['user_pname'].'|'.$status."\n";
			}
		}
		exit;
	break;   
	 default:
        break;
}

exit;
?>
